package com.lcy.utils;

import com.lcy.dao.SmbmsUserDao;
import com.lcy.entry.SmbmsUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    public static SqlSession getSqlSession() {
        String config = "mybatis-config.xml";
        SmbmsUser smbmsUser = null;
        try {
            //读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(config);
            //创建工厂建造者
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //通过工厂建造者，创建工厂
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
            //通过工厂，创建sqlsession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            return sqlSession;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeSqlSession(SqlSession sqlSession) {
        sqlSession.close();
    }


}
